package com.ts.api.http.market.channel;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarket;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarketConvertType;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.market.entity.ChannelMarketParameter;
import com.ts.api.module.api.convert.market.handler.ChannelMarketConvertHandler;
import com.ts.api.module.api.entity.ApiUserRequest;

import java.util.Map;

/**
 *  惠分期
 *
 *  飞书链接：https://ycncwx7jq9fw.feishu.cn/wiki/TiNFwqzgRif7IRkbUY2cygm8ned?from=from_copylink   密码：#7C73621
 *
 *  @author WJB
 */
@ApiChannelMarket(uniqueCode = "25fa56cec4c04d6f8eddbd720997b3b6", type = ApiChannelMarketConvertType.FEDERATE_LOGIN)
public class HuiFenQiFederate implements ChannelMarketConvertHandler {

    private static final String KEY = "6454A9AFCC693CAA";

    @Override
    public ApiUserRequest parse(RequestVO requestVO) {
        String body = requestVO.getStr("data");
        AES aes = AES.createEcb(KEY.getBytes());
        String bodyData = aes.decryptFromBase64Str(body);
        JSONObject object = JSONObject.parseObject(bodyData);
        String mobile = object.getString("phone");

        ApiUserRequest apiUserRequest = new ApiUserRequest();
        apiUserRequest.setPhone(mobile);
        return apiUserRequest;
    }

    @Override
    public Map<String, Object> response(ChannelMarketParameter channelMarketParameter, ApiBizStatus apiBizStatus) {
        if (apiBizStatus.hasBizSuc()) {
            return MapUtils.newHashMap(newMap -> {
                newMap.put("code", 200);
                newMap.put("data", MapUtils.newLinkedHashMap(dataMap -> {
                    dataMap.put("result", 1);
                    dataMap.put("remark", apiBizStatus.getBizMsg());
                    dataMap.put("url", channelMarketParameter.getUrl());
                }));
            });
        }

        return MapUtils.newHashMap(newMap -> {
            newMap.put("code", 500);
            newMap.put("remark", apiBizStatus.getBizMsg());
            newMap.put("result", 2);
        });
    }
}
